package iut.LocateWind.fr;
import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;

public class AudioRecorder {
	final MediaRecorder recorder = new MediaRecorder();
	final String path;

	public AudioRecorder(String path){
		this.path=path;
	}
	
	public void start() throws IOException{
		String state = android.os.Environment.getExternalStorageState();
		if(!state.equals(android.os.Environment.MEDIA_MOUNTED))
			throw new IOException("La carte SD n'est pas montée. Etat : "+state+".");
		File directory = new File(path).getParentFile();
		if(!directory.exists() && !directory.mkdir())
			throw new IOException("Le chemin vers le fichier n'a pas pu être crée.");
		
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setOutputFile(path);
		recorder.setMaxDuration(60*1000); // 1 minute
		recorder.prepare();
		recorder.start();
	}
	
	public void stop() throws IOException{
		recorder.stop();
		recorder.reset();
	}

	public Runnable getAmplitueRunnable = new Runnable(){
		public void run(){
			getMaxAmplitude();
		}
	};
	
	public int getMaxAmplitude() {
		return recorder.getMaxAmplitude();
	}
}
